import React, {Component} from 'react';
import store from "../../redux/store";
import {createIncrementAction, createDecrementAction, createIncrementAsyncAction} from "../../redux/count_action";

class Index extends Component {
    // componentDidMount() {
    //     store.subscribe(() => {
    //         this.setState({})
    //     })
    // }

    increment = () => {
        const {value} = this.selectNum
        store.dispatch(createIncrementAction(Number(value)))
    }
    decrement = () => {
        const {value} = this.selectNum
        store.dispatch(createDecrementAction(Number(value)))
    }
    incrementIfOdd = () => {
        const {value} = this.selectNum
        if (store.getState() % 2 !== 0)
            store.dispatch(createIncrementAction(Number(value)))
    }
    incrementAsnc = () => {
        const {value} = this.selectNum
        store.dispatch(createIncrementAsyncAction(Number(value), 500))
    }

    render() {
        return (
            <div>
                <h1>当前求和为：{store.getState()}</h1>
                <select ref={c => {
                    this.selectNum = c
                }}>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
                <button onClick={this.increment}>+</button>
                <button onClick={this.decrement}>-</button>
                <button onClick={this.incrementIfOdd}>当前求和为奇数再加</button>
                <button onClick={this.incrementAsnc}>异步加</button>
            </div>
        );
    }
}

export default Index;